package com.dkm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.dkm.entity.DynamicMessage;
import com.dkm.entity.Student;
import com.dkm.repository.DynamicMessageRepository;
import com.dkm.repository.StudentRepository;
import com.dkm.repository.UsersRepository;
import com.dkm.service.DynamicMessageService;
import com.dkm.util.ResultUtil;
import com.dkm.websocket.MyWebSocket;

@Service
public class DynamicMessageServiceImpl implements DynamicMessageService {

	//注入动态消息持久层
	@Autowired
	private DynamicMessageRepository dynamicMessageRepository;
	//注入学生持久层
	@Autowired
	private StudentRepository studentRepository;
	//注入websocket
	@Autowired
	private MyWebSocket myWebSocket;
	
	@Transactional //开启事务
	@Override
	public ResultUtil addDynamicMessage(String sid,DynamicMessage dynamicMessage) {
		//根据id获取学生信息
		Student student=studentRepository.findById(sid).get();
		
		if(student==null) {
			return new ResultUtil(-1, "发送失败，学生数据异常", null);
		}
		
		if(student.getCounselor()==null) {
			return new ResultUtil(-1, "发送失败，该学生未分配咨询师", null);
		}
		//设置学生信息
		dynamicMessage.setStudent(student);
		//接收人
		dynamicMessage.setRecipient(student.getCounselor().getCounselor_name());
		
		dynamicMessage.setCreate_time(new Date());
		//设置未读
		dynamicMessage.setIs_read("否");
		
		
		DynamicMessage dynamicMessage2=dynamicMessageRepository.saveAndFlush(dynamicMessage);
		if(dynamicMessage2!=null) {
			
			String message=dynamicMessage.getMessage_content()+"|"+student.getCounselor().getUser().getUser_id();
			//发送消息
			myWebSocket.sendToUser(message);
			return new ResultUtil(0, "发送成功", dynamicMessage2);
		}
		return new ResultUtil(-1, "发送失败", null);
	}

	@Override
	public Page<DynamicMessage> findAllDynamicMessages(Map<String, Object> map, Pageable pageable) {
		
		return dynamicMessageRepository.findAll(this.get(map), pageable);
	}

	private Specification<DynamicMessage> get(Map<String, Object> map) {
		
		return new Specification<DynamicMessage>() {
			@Override
			public Predicate toPredicate(Root<DynamicMessage> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				//1、创建Predicate对象,动态sql表达式
				Predicate predicate=criteriaBuilder.conjunction();
				//2、创建一个集合，存放多个条件,动态sql表达式集合
				List<Expression<Boolean>> expressions=predicate.getExpressions();
				
				//接收人
				if(!StringUtils.isEmpty(map.get("recipient"))) {
					expressions.add(criteriaBuilder.equal(root.get("recipient"), map.get("recipient")));
				}
				
				return predicate;
			}
		};
	}

	@Transactional //开启事务
	@Override
	public ResultUtil updateDynamicMessage(DynamicMessage dynamicMessage) {
		
		//根据id获取动态消息信息
		DynamicMessage oldData=dynamicMessageRepository.findById(dynamicMessage.getMessage_id()).get();
		
		//设置为已读
		oldData.setIs_read("是");
		
		//修改
		DynamicMessage dynamicMessage2=dynamicMessageRepository.saveAndFlush(oldData);
		
		if(dynamicMessage2!=null) {
			return new ResultUtil(0, "修改成功", dynamicMessage2);
		}
		return new ResultUtil(-1, "修改失败", null);
	}

	@Override
	public List<DynamicMessage> getUnreadDynamicMessage(String login_name) {
		
		return dynamicMessageRepository.getUnreadDynamicMessage(login_name);
	}

}
